<template>
  <el-dropdown trigger="click" @command="handleSetLanguage">
    <span class="el-dropdown-link">
      {{ currentLangName }}<i class="el-icon-arrow-down el-icon--right"></i>
    </span>
    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item :disabled="language==='zh'" command="zh">
        中文
      </el-dropdown-item>
      <el-dropdown-item :disabled="language==='en'" command="en">
        English
      </el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
const allLanguage = { 'zh': '中文', 'en': 'English' }
export default {
  computed: {
    language() {
      let lang = this.$store.getters.language
      if (lang === '' || lang === undefined) {
        lang = 'zh'
        this.handleSetLanguage(lang)
      }
      return lang
    },
    currentLangName() {
      let lang = this.$store.getters.language
      if (lang === '' || lang === undefined) {
        lang = 'zh'
      }
      return allLanguage[lang]
    },
    langOptions() {
      return allLanguage
    }
  },
  methods: {
    handleSetLanguage(lang) {
      this.$i18n.locale = lang
      this.$store.dispatch('app/setLanguage', lang)
      this.$message({
        message: 'Switch Language Success',
        type: 'success'
      })
    }
  }
}
</script>
